package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.entity.Cart;
import cn.kgc.kgde.bookstore.entity.Cartitem;
import cn.kgc.kgde.bookstore.entity.ResultInfo;
import cn.kgc.kgde.bookstore.mapper.CartMapper;
import cn.kgc.kgde.bookstore.mapper.CartitemMapper;
import cn.kgc.kgde.bookstore.service.CartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import sun.plugin.dom.core.Element;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2022-05-23
 */
@Controller
@RequestMapping("/cartitem")
public class CartitemController {

    @Resource
    private CartMapper cartMapper;
    @Resource
    private CartService cartService;
    @Resource
    private CartitemMapper cartitemMapper;

    //清空购物车
    @DeleteMapping("delete")
    public ResultInfo delete(String userCode, Long cartId){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode",userCode);
        List<Cart> cartList = this.cartMapper.selectList(wrapper);
        System.out.println("cartList = " + cartList);

        QueryWrapper<Cartitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cartId",cartId);
        this.cartitemMapper.delete(queryWrapper);
        return new ResultInfo(200,"已经清空了");
    }

    //购物车根据id删除商品
    @DeleteMapping("deleteById")
    public ResultInfo deleteById(Cartitem cartitem){
        this.cartitemMapper.deleteById(cartitem.getCartItemId());
        return new ResultInfo(200,"删除该商品");
    }

}
